﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Media;
using DataForest.Utilities;

namespace DataForest.DataModel
{
    public class Workgroup : NotifyPropertyChanged
    {

        public Workgroup()
        {
            this.Table = new Table();
            this.Table.Columns.Add("Spalte 1", typeof(string));
            this.Trees = new ObservableCollection<Tree>();
        }

        public Workgroup(Table table)
        {
            this.Table = table;
            this.Trees = new ObservableCollection<Tree>();
        }

        public event NotifyCollectionChangedEventHandler TreeCollectionChanged;

        private Table table;
        private ObservableCollection<Tree> trees;
        private List<string> targetSet;

        private static Color[] colors = new Color[] { Colors.Red, Colors.Blue, Colors.Green, Colors.Yellow, Colors.LightPink };
        private Queue<Color> colorQueue = new Queue<Color>(colors);

        private Dictionary<string, Color> colorMap = new Dictionary<string, Color>();
      
        public Brush BackgroundBrush { get; set; }

        public Color GetAssociatedColor(string value) {
            if (this.colorMap.ContainsKey(value))
            {
                return this.colorMap[value];
            }
            else
            {
                Color color = colorQueue.Dequeue();
                colorMap.Add(value, color);
                colorQueue.Enqueue(color);
                return color;
            }
        }

        public void RefreshTable()
        {
            OnPropertyChanged("Table");
        }

        public Table Table
        {
            get
            {
                return table;
            }
            set
            {
                if (table != value)
                {
                    table = value;
                    OnPropertyChanged("Table");
                }
            }
        }
        public ObservableCollection<Tree> Trees
        {
            get
            {
                return trees;
            }
            set
            {
                if (value != trees)
                {
                    if (trees != null)
                    {
                        trees.CollectionChanged -= HandleTreeCollectionChanged;
                    }
                    trees = value;
                    if (trees != null)
                    {
                        trees.CollectionChanged += HandleTreeCollectionChanged;
                    }
                    OnPropertyChanged("Trees");
                }
            }
        }
        public List<string> TargetSet
        {
            get
            {
                return targetSet;
            }
            protected set
            {
                if (targetSet != value)
                {
                    targetSet = value;
                    OnPropertyChanged("TargetSet");
                }
            }
        }

        // if tree collection has changed fire TreeCollectionChanged event
        private void HandleTreeCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            NotifyCollectionChangedEventHandler handler = TreeCollectionChanged;
            System.Diagnostics.Debug.WriteLine("Workgroup: TreeCollection Changed");
            if (handler != null)
            {
                handler(this, e);
            }
        }

    }
}